package com.lskj.shiro.exception;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;


/**
 * @Author zzq
 * @Date 2020/3/31 16:31
 * @Version 1.0
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger= LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler({AuthenticationException.class,UnauthorizedException.class})
    public String shiroException(Exception e, Model model) {
        logger.error(e.getMessage());
        model.addAttribute("message",e.getMessage());
        return "error";
    }

    @ExceptionHandler(MyException.class)
    public String myExceptionHandle(MyException e,Model model){
        logger.error(e.getMessage());
        model.addAttribute("message",e.getMessage());
        return "error";
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public String handlerNoFoundException(Exception e,Model model) {
        logger.error(e.getMessage(), e);
        model.addAttribute("message","路径不存在，请检查路径是否正确");
        return "error";
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public String httpRequestMethodNotSupportedException(Exception e,Model model){
        logger.error(e.getMessage(), e);
        model.addAttribute("message",e.getMessage());
        return "error";
    }

    @ExceptionHandler
    public String exceptionHandle(Exception e,Model model){
        logger.error(e.getMessage());
        model.addAttribute("message",e.getMessage());
        return "error";
    }
}
